from . import text_helper, translate
from config import Config

class Analyse:
    def __init__(self) -> None:
        self.text_helper = text_helper.JiayanTextHelper()
        self.translator = translate.BaiduTranslator()

    def init(self, config:Config) -> None:
        self.translator.init(config)
    
    def config(self, config:dict) -> None:
        self.translator.config(config)

    def get(self, content:str) -> list:
        split = self.text_helper.get_data(content)
        def trans(types:list=None): # 只翻译特定类型的字词。
            result = []
            cost_token = 0
            for text, text_type in split:
                if not types or text_type in types:
                    after = ""
                    trans_result = self.translator.translate(text).get("trans_result", None)
                    if trans_result: 
                        after = trans_result[0]["dst"]
                        cost_token+=1

                    result.append(
                        {
                            "original": text,
                            "after": after
                        }
                    )
                else: 
                    result.append({"original":text})

            return result, cost_token
        return split, trans()[0]
    
    def get_cost_token(self, content:list, types:list):
        cost_token = 0
        for _, text_type in content:
            if text_type in types:
                cost_token += 1
        return cost_token

            
